package br.ufrj.dcc.smarthotel.controls;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import br.ufrj.dcc.smarthotel.R;
import br.ufrj.dcc.smarthotel.activities.InvitePeopleActivity;
import br.ufrj.dcc.smarthotel.entities.EventUser;

import com.google.gson.reflect.TypeToken;


public class InvitePeopleControl extends Control {
	private InvitePeopleActivity activity;
	
	public InvitePeopleControl(InvitePeopleActivity invitePeopleActivity) {
		this.activity = invitePeopleActivity;
	}

	@SuppressWarnings("unchecked")
	public void loadAvailablePeople(int clickedEventId) {
		Type listType = new TypeToken<List<EventUser>>() {}.getType();
		List<EventUser> eventUserList = (List<EventUser>) this.retrieve(this.baseURI + "event/" + clickedEventId + "/notinviteduser", listType);
		
		this.activity.setEventUserList(eventUserList);
	}

	public void inviteEventUser(EventUser clickedEventUser) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		
		parameters.put("user", clickedEventUser.getUser());
		parameters.put("isUserConfirmed", false);
		parameters.put("isUserManager", false);
		
		this.doPost("event/" + this.activity.getClickedEventId() + "/user", parameters);
		
		this.activity.showQuickMessage(R.string.msg_UserInvited);
		
		Intent intent = new Intent(this.activity.getApplicationContext(), InvitePeopleActivity.class);
    	Bundle bundle = new Bundle();
    	
    	bundle.putString("clickedEventId", String.valueOf(this.activity.getClickedEventId()));
    	
    	intent.putExtras(bundle);
		
		this.activity.setResult(1, intent);
		
		this.activity.finish();
	}

}
